package com.chen.web;

import com.chen.pojo.PageBean;
import com.chen.pojo.Visit;
import com.chen.service.VisitService;
import com.chen.utils.DateJsonValueProcessor;
import com.chen.utils.ResponseUtil;
import com.chen.utils.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * com.example.demo.controller
 * 作者      : 陈晓
 * 描述      :
 * 创建日期  : 2018/4/27
 * 修改日期  :
 */

@Controller
@RequestMapping("/visit")
public class VisitController {

  @Autowired
  private VisitService visitService;

  private Logger logger = Logger.getLogger(VisitController.class);

  @RequestMapping("/index")
  public String index() {
    return "visit/visitList";
  }

  @RequestMapping("/list")
  public String list(Visit visit, HttpServletResponse response, HttpServletRequest request) {
    JSONObject result = new JSONObject();
    PageBean pageBean = new PageBean();
    String page = request.getParameter("page");
    String rows = request.getParameter("rows");
    if (StringUtil.isNotEmpty(page) && StringUtil.isNotEmpty(rows)) {
      pageBean.setPage(Integer.parseInt(page));
      pageBean.setPageSize(Integer.parseInt(rows));
    }
    try {
      List<Visit> list = visitService.getList(visit, pageBean);
      long count = visitService.getCount(visit);
      JsonConfig jsonConfig=new JsonConfig();
      jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
      JSONArray array=JSONArray.fromObject(list, jsonConfig);
      result.put("rows", array);
      result.put("total", count);
      ResponseUtil.write(response, result);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  @RequestMapping("/save")
  public String save(Visit visit,HttpServletResponse response){
    JSONObject result = new JSONObject();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String message = "访问记录操作成功!!!";
    try {//添加操作
      if(visit.getId() == null){
        visit.setDateentrytime(sdf.format(new Date()));
        visitService.insert(visit);
      }else {//修改操作
        visitService.update(visit);
      }
      result.put("success",true);
      result.put("message",message);
    } catch (Exception e) {
      result.put("success",false);
      result.put("message","访问记录操作失败!!!");
      ResponseUtil.write(response, result);
      logger.error(e.getCause().getMessage());
      e.printStackTrace();
    }
    ResponseUtil.write(response, result);
    return null;
  }

  /*@RequestMapping("/update")
  public String update( Visit visit, HttpServletResponse response){
    JSONObject result = new JSONObject();
    String message = "访问记录修改成功!!!";
    try {
      visitService.update(visit);
      result.put("success",true);
      result.put("message",message);
    } catch (Exception e) {
      result.put("success",false);
      result.put("message","访问记录修改失败!!!");
      ResponseUtil.write(response, result);
      logger.error(e.getCause().getMessage());
      e.printStackTrace();
    }
    ResponseUtil.write(response, result);
    return null;
  }*/
}
